<?php

namespace App\Services\CountTodayGoldBonus;

use Carbon\Carbon;
use App\Models\User;
use App\Jobs\CountTodayBonusJob;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Builder;

class CountTodayGoldBonus
{
	protected function execute()
	{
		User::query()
			->whereHas('goldRecords', function (Builder $query) {
				$query->whereNotNull('paid_at')->whereDate('created_at', Carbon::yesterday());
			})->chunks(50, function (Collection $users) {
			
				if ($users->isEmpty()) return false;
				
				foreach ($users as $user) {
					$queue = app()->environment('local') ? 'default' : 'gold_storm_reward';
					dispatch(new CountTodayBonusJob($user))->onQueue($queue);
				}
				
				return true;
			});
	}
	
	public function __invoke()
	{
		$this->execute();
	}
}
